package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.UserBankInfo;
import cn.wolfcode.p2p.business.mapper.UserBankInfoMapper;
import cn.wolfcode.p2p.business.service.IUserBankInfoService;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserBankInfoServiceImpl implements IUserBankInfoService {
        @Autowired
        private UserBankInfoMapper userBankInfoMapper;
        @Autowired
        private IUserInfoService userInfoService;

        @Override
        public List<UserBankInfo> listMybank(long loginInfoId) {
                return userBankInfoMapper.selectUserBankInfoByLoginInfoId(loginInfoId);
        }

        @Override
        public void save(UserBankInfo userBankInfo) {
                //判断基本参数吧
                AssertUtil.instance().isNotNull(userBankInfo,"请别捣乱!");
                UserBankInfo ubf = new UserBankInfo();
                LoginInfo loginInfo = UserContext.getLoginInfo();
                ubf.setBankForkName(userBankInfo.getBankForkName());
                ubf.setBankName(userBankInfo.getBankName());
                ubf.setAccountNumber(userBankInfo.getAccountNumber());
                ubf.setLoginInfo(loginInfo);
                userBankInfoMapper.insert(ubf);
                UserInfo userInfo = userInfoService.getById(loginInfo.getId());
                userInfo.setBankInfoId(ubf.getId());
                userInfoService.update(userInfo);
        }

        @Override
        public UserBankInfo selectByid(Long id) {
                return userBankInfoMapper.selectByPrimaryKey(id);
        }
}
